GMountOperation on OpenBSD: remove the need for kvm(3)
authorAntoine Jacoutot <ajacoutot@openbsd.org>
Sat, 15 Oct 2011 09:27:47 +0000 (11:27 +0200)
committerAntoine Jacoutot <ajacoutot@openbsd.org>
Mon, 17 Oct 2011 17:06:41 +0000 (19:06 +0200)
kvm(3) is considered a deprecated interface, so make
GMountOperation::show-processes use the recommended sysctl(3) interface
instead. This also removes the need to link with libkvm.

https://bugzilla.gnome.org/show_bug.cgi?id=661835

configure.ac
gtk/gtkmountoperation-x11.c

index 8944f6efd1d3d4011687581ee7cad934571b236b..a73cdc72a89d3e1f560fe77d1441ee963ac2328a 100644 (file)
@@ -165,9 +165,6 @@ case $host in
   *-*-linux*)
     os_linux=yes
     ;;
-  *-*-openbsd*)
-    os_openbsd=yes
-    ;;
 esac
 
 dnl
@@ -1314,9 +1311,6 @@ if test "x$enable_x11_backend" = xyes; then
   GTK_PACKAGES="$GTK_PACKAGES pangoft2"
 fi
 GTK_EXTRA_LIBS=
-if test x"$os_openbsd" = xyes; then
-  GTK_EXTRA_LIBS="$GTK_EXTRA_LIBS -lkvm"
-fi
 
 GTK_EXTRA_CFLAGS=
 GTK_DEP_LIBS="$GDK_EXTRA_LIBS $GTK_DEP_LIBS_FOR_X `$PKG_CONFIG --libs $PANGO_PACKAGES $GTK_PACKAGES_FOR_X $GTK_PACKAGES` $GTK_EXTRA_LIBS $MATH_LIB"
index 52bce316a47ee47e2c70fdd7d0721a59352494f9..69ccc3a019c6aa5aad2d7b127484757bda016978 100644 (file)
@@ -43,8 +43,8 @@
 #include <errno.h>
 
 #if defined(__OpenBSD__)
+#include <stdlib.h>
 #include <sys/param.h>
-#include <kvm.h>
 #include <fcntl.h>
 #include <sys/sysctl.h>
 #endif
@@ -726,46 +726,44 @@ pid_get_command_line (GPid pid)
 static GPid
 pid_get_parent (GPid pid)
 {
-  struct kinfo_proc *proc;
-  int count;
-  kvm_t *kvm;
-  GPid ppid = 0;
+  struct kinfo_proc kp;
+  size_t len;
+  GPid ppid;
+
+  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid,
+                sizeof(struct kinfo_proc), 0 };
 
-  kvm = kvm_openfiles (NULL, NULL, NULL, KVM_NO_FILES, NULL);
-  if (kvm == NULL)
-    return 0;
+  if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) == -1)
+      return (-1);
+  mib[5] = (len / sizeof(struct kinfo_proc));
 
-  proc = kvm_getprocs (kvm, KERN_PROC_PID, pid, sizeof(struct kinfo_proc), &count);
-  if (count == 1)
-    ppid = proc->p_ppid;
+  if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) < 0)
+      return -1;
+
+  ppid = kp.p_ppid;
 
-  kvm_close (kvm);
   return ppid;
 }
 
 static gchar *
 pid_get_env (GPid pid, const gchar *key)
 {
-  kvm_t *kvm;
-  struct kinfo_proc *proc;
-  char **strs;
+  size_t len = PATH_MAX;
+  char **strs = NULL;
   char *ret;
   char *end;
   int key_len;
-  int count;
   int i;
 
-  kvm = kvm_openfiles (NULL, NULL, NULL, KVM_NO_FILES, NULL);
-  if (kvm == NULL)
-    return NULL;
+  int mib[] = { CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ENV };
+
+  strs = (char **)realloc(strs, len);
 
   key_len = strlen (key);
 
   ret = NULL;
-  proc = kvm_getprocs (kvm, KERN_PROC_PID, pid, sizeof(struct kinfo_proc), &count);
-  if (proc != NULL)
+  if (sysctl(mib, nitems(mib), strs, &len, NULL, 0) != -1)
     {
-      strs = kvm_getenvv (kvm, proc, 0);
       for (i = 0; strs[i] != NULL; i++)
        {
          if (g_str_has_prefix (strs[i], key) && (*(strs[i] + key_len) == '='))
@@ -780,35 +778,33 @@ pid_get_env (GPid pid, const gchar *key)
        }
     }
 
-  kvm_close (kvm);
+  g_free (strs);
   return ret;
 }
 
 static gchar *
 pid_get_command_line (GPid pid)
 {
-  kvm_t *kvm;
-  struct kinfo_proc *proc;
-  int count;
-  char **strs;
-  char *ret;
+  size_t len = PATH_MAX;
+  char **strs = NULL;
+  char *ret = NULL;
   char *end;
 
-  kvm = kvm_openfiles (NULL, NULL, NULL, KVM_NO_FILES, NULL);
-  if (kvm == NULL)
-    return NULL;
+  int mib[] = { CTL_KERN, KERN_PROC_ARGS, pid, KERN_PROC_ARGV };
 
-  proc = kvm_getprocs (kvm, KERN_PROC_PID, pid, sizeof (struct kinfo_proc), &count);
-  if (proc == NULL)
-    return NULL;
+  strs = (char **)realloc(strs, len);
+
+  if (sysctl(mib, nitems(mib), strs, &len, NULL, 0) == -1) {
+    g_free (strs);
+    return ret;
+  }
 
-  strs = kvm_getargv (kvm, proc, 0);
   ret = g_strjoinv (" ", strs);
   /* skip invalid UTF-8 */
   if (!g_utf8_validate (ret, -1, (const gchar **) &end))
     *end = '\0';
 
-  kvm_close (kvm);
+  g_free (strs);
   return ret;
 }